#!/bin/bash
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::                                                                            ::
#::                                                                            ::
#:: Program name : PDPACK.BAT                                                  ::
#::                                                                            ::
#:: Version      : 6.00                                                        ::
#::                                                                            ::
#:: Copyright    : IBM Licensed Materials                                      ::
#::                                                                            ::
#::                All Rights Reserved  - Property of IBM                      ::
#::                                                                            ::
#::                5724-B16 (C) Copyright IBM Corp. 2000, 2002                 ::
#::                                                                            ::
#::                                                                            ::
#::                                                                            ::
#:: Contents     : PD Information Packager                                     ::
#::                                                                            ::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::                                                                            ::
#:: Change Activity:                                                           ::
#::                                                                            ::
#:: Flag Reason  Rlse  Date  Origin  Description                               ::
#:: ---- ------  ---- ------ ------  ----------------------------------------- ::
#:: $P0= 004576  0600 021023 HDMC :  amend copyright statement                 ::
#::                                                                            ::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


#
# print_copyright
#     This function prints out the correct copyright statement 
#
print_copyright ()
{
    echo
    echo "IBM (TM) LAN Distributed Platform Version 6.0 - LANDP"
    echo "5724-B16 (C) Copyright IBM Corp. 2000, 2002"
    echo "Licensed Materials - Property of IBM - All rights reserved."
    echo "(TM) Trademark of International Business Machines Cororation."
    echo "LANDP is a trademark of International Business Machines Corporation."
    echo "-------------------------------------------------------------------"
}


#
# print_usage
#     This function prints out the available options for pdpack
#
print_usage ()
{
    print_copyright
    
    echo "LANDP PD Information Packager"
    echo "-----------------------------"
    echo "This utility collects information required for LANDP Problem "
    echo "Determination from a failing workstation.  It produces a     "
    echo "compressed tar file containing this information which should be"
    echo "sent to IBM to help diagnose the problem.  "
    echo
    echo "pdpack must be run from a shell prompt in the failing "
    echo "workstation's directory.  The shell must have the LANDP "
    echo "enviornment variables set.  "
    echo
    echo "pdpack creates a log file called packlog.txt, that can"
    echo "be viewed to see what information has been included in the "
    echo "archive." 
    echo
    echo "If you wish to view the contents of the archive, please copy the"
    echo "archive to a new directory and extract the files with a suitable"
    echo "command."
    echo
    echo "usage:  pdpack [options]"
    echo 
    echo "Options:"
    echo "    -q,--quiet                      Quiet mode selected, no user interaction"
    echo "    -p,--pmr pmr_number             PMR number input. Use if given a PMR number"
    echo "    -i,--interactive full | none    Prompt user for interaction"
    echo "    --help                          Prints out this description"
    
}


#
# get_parameters
#     This function parses the input parameter list 
#     and sets the necessary variables.
# 
get_parameters ()
{
    while [ -n "$1" ]
    do
       case $1 in
          -i)            shift; interactive=$1;;
          -p)            shift; inPMR=$1 ;;
          -q)            verbosemode="n"; interactive="none";;
          --help)        print_usage; exit ;;
          --quiet)       verbosemode="n"; interactive="none" ;;
          --pmr)         shift; inPMR=$1 ;;
          --interactive) shift; interactive=$1;;
          *)             print_usage; exit ;;
       esac
       shift
    done

}

 
# 
# find_ws_name 
#     Greps for the phrase "loader spv" from within autofbss
#     and places the output in /tmp/pdfindspv.txt.  The output
#     is then read in to three variables.  The thrid should 
#     contain the workstationg ID. 
#
find_ws_name()
{
    if grep -i "loader spv" autofbss > /tmp/pdfindspv.txt
    then
       # Now extract the the workstation name from packinfo
       read loader server parm < /tmp/pdfindspv.txt
       parm=${parm#-}
       WSID=${parm#/}
       rm -rf /tmp/pdfindspv.txt
    else
       WSID=""
    fi
}


#
# set_pmr_num
#     This function validates the given PMR number 
#
set_pmr_num ()
{
    pmr=""
    if [ -n "$inPMR" ]   
    then
        pmr=$inPMR
        # Validate PMR number is 5 digits long
        if [ "${#pmr}" -ne 5 ]
        then
            echo "PMR Number must be five digits long"
            exit
        fi
    fi   
}


# 
# build_pack_name
#     The package name is built using the PMR if it exists.
#     If it does not exist them the WSID is used.  And if 
#     that does not exist then pdoutput is used.
# 
build_pack_name()
{
    if [ -n "$pmr" ] 
    then
    	packname="pd$pmr"
    else
	if [ -n "$WSID" ]
	then
	    packname="pdpack$WSID"
        else
	    packname="pdoutput"
	fi
    fi
}


#
# clean_up_directories
#     This removes the directory structure created.
#     
clean_up_directories ()
{
    if [ $interactive = "full" ]
    then
        echo -n "Can pdpack remove $packname/sysout, $packname/landp and $packname (y or n)? " 
        read reply
    	: ${reply:="n"}

    	if [ $reply != "y" ] && [ $reply != "Y" ]
    	then
            echo "Stopping pdpack...."
            exit
    	fi
    fi

    if [ -d $packname ] 
    then
        rm -rf $packname
    fi
}


#
# init_pdpack
#     This function controls the initialisation of pdpack. 
#     The function creates the directory structure and the
#     log file
#     
init_pdpack ()
{
    inPMR=""
    verbosemode="y"
    interactive="full"

    get_parameters $@

    print_copyright 
    
    # Check that spv.cfg exist in the current working directory.
    # Check that we have a supervisor accessible from this 
    # Shell.  Use the which command.
    if [ -e spv.cfg ]
    then
        which spv > /dev/null 2>&1
        if [ $? -ne 0 ] ; then
      	    echo "pdpack must be run from within a shell that has access to LANDP." 
            exit
        fi
    else
	echo "pdpack must berun from the LANDP workstation directory." 
        exit 
    fi

    set_pmr_num
    find_ws_name
    build_pack_name
    
    # Check previous executions of pdpack have cleaned up 
    if [ -e $packname ]
    then
        if [ $verbosemode = "y" ]
        then
	    echo "Attempting to clean up previous pdpack run."
        fi
        clean_up_directories 
    fi

    # Create Temporary directories/
    if [ $verbosemode = "y" ]
    then
        echo "Making pdpack temporary directories."
    fi
 
    #  If we are in interactive mode, then each directory creation must be agreed 
    if [ $interactive = "full" ]
    then
        echo -n "Can pdpack make temporary directory $packname (y or n) ? " 
        read reply
    	: ${reply:="n"}

    	if [ $reply != "y" ] && [ $reply != "Y" ]
        then
            echo "Stopping pdpack...."
            exit
        else
            # create directory
            mkdir $packname > /dev/null
            if [ $? -ne 0 ]
            then
                echo "Could not create $packname.  Stopping pdpack"
                exit
            fi
        fi
    
        echo -n "Can pdpack make temporary directory $packname/sysout (y or n) ? " 
        read reply
    	: ${reply:="n"}

    	if [ $reply != "y" ] && [ $reply != "Y" ]
        then
            echo "Stopping pdpack...."
            exit
        else
            # create directory
            mkdir $packname/sysout > /dev/null
            if [ $? -ne 0 ]
            then
                echo "Could not create $packname/sysout.  Stopping pdpack"
                exit
            fi
        fi

        echo -n "Can pdpack make temporary directory $packname/landp (y or n) ? " 
        read reply
    	: ${reply:="n"}

    	if [ $reply != "y" ] && [ $reply != "Y" ]
        then
            echo "Stopping pdpack...."
            exit
        else
            # create directory
            mkdir $packname/landp > /dev/null
            if [ $? -ne 0 ]
            then
                echo "Could not create $packname/sysout.  Stopping pdpack"
                exit
            fi
        fi
    else
        # We are not in interactive mode, create all directories.
        mkdir $packname
	if [ $? -ne 0 ]
	then
	    echo "Could not create $packname.  Stopping pdpack"
	    exit
	fi

        mkdir $packname/sysout
	if [ $? -ne 0 ]
	then
	    echo "Could not create $packname/sysout.  Stopping pdpack"
	    exit
	fi

        mkdir $packname/landp > /dev/null
        if [ $? -ne 0 ]
        then
            echo "Could not create $packname/landp.  Stopping pdpack"
            exit
        fi
    fi


    # Set up pdpack log (packlog.txt)
    logName="$packname/packlog.txt"
   
    echo "LANDP_PD Information Packager Log" > $logName
    echo "---------------------------------" >> $logName
    echo "This file descirbes the information collected by pdpack " >> $logName
    echo "the last time it was run.  It can be deleted if this log" >> $logName
    echo "data is no longer required." >> $logName
    echo "--------------------------------------------------------" >> $logName
    echo "If a file is 'not collected' the copy process failed." >> $logName
    echo "-----------------------------------------------------" >> $logName

    date >> $logName
    echo "Operating System : Linux " >> $logName

    if [ -n "$pmr" ] 
    then
        echo "PMR Number = " $pmr >> $logName
    else
        echo "No PMR Number given" >> $logName
    fi
    
    if [ -n "$WSID" ]
    then
        echo "Workstation ID = " $WSID >> $logName
    else
        echo "Could not find Workstation ID - no autofbss ?" >> $logName
    fi

    echo "Temporary directories created : $packname, $packname/sysout, $packname/landp" >> $logName
}

                     
#
# gather_details
#     This function gets most of the informatoin that we are interested in
#                     
gather_details()
{
    echo "--------------" >> $logName
    echo "SYSTEM DETAILS" >> $logName
    echo "--------------" >> $logName

    #Get Environment Variables
    echo "EHC_MAP_LOC = " $EHC_MAP_LOC > $packname/env.var
    echo "LD_LIBRARY_PATH = " $LD_LIBRARY_PATH >> $packname/env.var
    echo "NLSPATH = "$NLSPATH >> $packname/env.var
    echo "LOGPATH = " $LOGPATH >> $packname/env.var
    echo "LOCKSPATH = " $LOCKSPATH >> $packname/env.var

    echo "Created env.var to contain LANDP related environment variable information." >> $logName

    # Run version and move output to $packname/sysout
    rm -f version.txt > /dev/null 2>&1

    version > /dev/null
    if [ -e version.txt ]
    then
        cp version.txt $packname/sysout > /dev/null
    	echo "Version run and version.txt copied to $packname/sysout." >> $logName
    else
        echo "Version run but no version.txt was created." >> $logName
    fi

    # System Utilities
    # uname - Provides the Linux kernal version and build details
    # env - lists the environment settings
    # lsb_release - indicates whether lsb compliant platform
    uname -a > $packname/sysout/uname.txt 2>&1
    env > $packname/sysout/env.txt 2>&1
    lsb_release -a >$packname/sysout/lsb.txt 2>&1

    if [ $verbosemode = "y" ]
    then 
        echo "Ran some system utilities: uname, env, and lsb_release. " 
        echo "uname.txt, env.txt, and lsb.txt have been created in $packname/sysout."
    fi
    echo "Ran some system utilities: uname, env, and lsb_release. " >> $logName
    echo "uname.txt, env.txt, and lsb.txt have been created in $packname/sysout." >> $logName

    # Now copy LANDP files to the LANDP directory
    echo "" >> $logName
    echo "-----------" >> $logName
    echo "LANDP FILES" >> $logName
    echo "-----------" >> $logName
    
    for fname in autofbss *.cfg *.sys *.ini *.pch ehc*.lgd ehc*.trp ehclog*.dat 
    do 
        if [ -f $fname ] 
        then
            cp $fname $packname/landp >> $logName 2>&1
            if [ $? -eq 0 ]
	    then
                echo "$fname collected" >> $logName
            else
	        echo "$fname not collected" >> $logName
	    fi
        else
            echo "$fname not found" >> $logName
        fi
    done
    
    for fname in *.dbd *.pcb *.se* *.pro *.btl *.log  
    do 
        if [ -f $fname ] 
        then
            cp $fname $packname/landp >> $logName 2>&1
            if [ $? -eq 0 ]
	    then
                echo "$fname collected" >> $logName
            else
	        echo "$fname not collected" >> $logName
	    fi
        else
            echo "$fname not found" >> $logName
        fi
    done

}

 
#
# package
#     This function creates the gzip tar archive
# 
package()
{
    tar --create --verbose --file=$packname.tar.gz --gzip $packname/* > /dev/null
    if [ $verbosemode = "y" ]
    then
        echo "$packname.tar.gz created. This is a tar archive created with the "
        echo "following options:  "
        echo "        --create --verbose --gzip"
        echo "To list the archive members use the following command:  "
        echo "        tar --list --ungzip --file=$packname.tar.gz"
        echo "To extract members use the following command:"
        echo "        tar --extract --ungzip --file=$packname.tar.gz <members name list>"
    fi
}


# The program                                                
init_pdpack $@
gather_details
package
clean_up_directories
